'use strict';

angular.module('afasApp')
  .controller('StudentDiscountsCtrl', ['$scope', 'Services', '$state', function ($scope, Services, $state) {

    $scope.items = [];
    $scope.params = {
      'getall': true,
      'include': 'student,discount_type',
      'is_valid': true,
      'student_name': ''
    };

    // PAGINATION
    $scope.pagesize = 10;

    $scope.pagination = {
      current: 1
    };

    $scope.pageChanged = function(newPage) {
      getResultsPage(newPage);
    };

    function getResultsPage(pageNumber) {
      var data = $scope.params;
      data.pagesize = $scope.pagesize;
      data.page = (pageNumber - 1);
      $scope.pagination.current = pageNumber;
      Services.getItems('student_discounts', data)
      .then(function( results ) {
        console.log(results.headers);
        var pagingData = angular.fromJson(results.headers['x-pagination']);
        $scope.totalItems = pagingData.TotalCount;
        $scope.items = results.data;
      });
    }
    // getResultsPage(1);

    $scope.loadSD = function() {
      Services.getItems('student_discounts', $scope.params)
      .then(function( results ) {
        var pagingData = angular.fromJson(results.headers['x-pagination']);
        $scope.totalItems = pagingData.TotalCount;
        $scope.items = results.data;
      });
    };

    $scope.loadSD();

    $scope.searchDiscount = function (){
      getResultsPage(1);
    };

    // function applyRemoteData( newData ) {
    //   $scope.countries = newData;
    // }

    // function loadRemoteData() {
    //   Services.getItems('countries')
    //   .then(function( results ) {
    //     applyRemoteData( results.data );
    //   });
    // }

    // loadRemoteData();
  }])
  .controller('StudentDiscountCreateCtrl', ['$scope', 'Services', '$state', '$stateParams', '$filter', function ($scope, Services, $state, $stateParams, $filter) {

    $scope.item = {
      'Student': {}
    };
    $scope.errors = {};

    $scope.Staffs = [];
    $scope.Clients = [];

    // SEARCH STUDENT TO REGISTER
    $scope.searchKey = $stateParams.key;
    console.log($stateParams);
    $scope.searchText = $scope.searchKey;
    $scope.params = {
      'is_valid': true,
      'keyword': $scope.searchText,
      'include': 'all'
    };
    $scope.searchStudent = function() {
      $state.go( '^.searchList', {key: $scope.searchText} );
    };
    $scope.DiscountTypes = [];

    // GET STUDENT ID
    $scope.item.Student.Id = $stateParams.id;

    $scope.pagesize = 10;

    $scope.pagination = {
        current: 1
    };

    $scope.pageChanged = function(newPage) {
      getResultsPage(newPage);
    };

    function getResultsPage(pageNumber) {
      var data = $scope.params;
      data.pagesize = $scope.pagesize;
      data.page = (pageNumber - 1);
      $scope.pagination.current = pageNumber;
      Services.getItems('students', data)
      .then(function( results ) {
        var pagingData = angular.fromJson(results.headers['x-pagination']);
        $scope.totalItems = pagingData.TotalCount;
        $scope.students = results.data;
      });
    };

    if ($stateParams.key) {
      getResultsPage(1);
    };

    $scope.selectStudent = function(index) {
      $state.go('^.new', {id: index});
      // $scope.ptNew.StudentId = index;
    };

    if ($scope.item.Student.Id) {
      Services.getItem('students', $scope.item.Student.Id, {'include': 'all'})
      .then(function(results){
        $scope.currStudent = results.data;
      });

      var DiscountTypeData = {
        'pagesize': 200
      };

      Services.getItems('discount_types', DiscountTypeData)
      .then(function(results){
        $scope.DiscountTypes = results.data;
      });
    };

    $scope.add = function () {
      $scope.errors = {};
      $scope.item.ValidFrom = $filter('date')($scope.item.ValidFrom, 'yyyy-MM-dd');
      $scope.item.ValidUntil = $filter('date')($scope.item.ValidUntil, 'yyyy-MM-dd');
      // console.log($scope.item);
      Services.add('student_discounts', $scope.item)
      .then(function(){
        $scope.item = {};
        $state.go( '^.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };

    $scope.ChangeDiscountType = function(){
      // Services.getItem('discount_types', $scope.item.DiscountType.Id)
      // .then(function(results){
        var data = $scope.item.DiscountType;
        console.log(data);
        $scope.item.Amount = data.DefaultAmount;
        // $scope.item.SourceReference = data.Id;
        $scope.item.Percentage = data.DefaultPercentage;
        $scope.item.ValidFrom = data.DefaultValidFrom;
        $scope.item.ValidUntil = data.DefaultValidUntil;

        console.log(data.DiscountSourceType);

        if (data.DiscountSourceType === 1) {
          console.log(data.DiscountSourceType);
          Services.getItems('staffs', {'getall': true, 'is_valid': true})
          .then(function(results){
            $scope.Staffs = results.data;
          });
        };

        if (data.DiscountSourceType === 2) {
          Services.getItems('clients', {'getall': true})
          .then(function(results){
            $scope.Clients = results.data;
          });
        };
      // })
    };
  }])
  .controller('CountryCtrl', ['$scope', 'Services', '$state', '$stateParams', function ($scope, Services, $state, $stateParams) {

    $scope.country = {};
    $scope.currentID = $stateParams.id;

    Services.getItem('countries', $stateParams.id)
    .then(function(results){
      $scope.country = results.data;
    });

    $scope.updateCountry = function () {
      Services.update('countries', $scope.country, $scope.currentID)
      .then(function(){
        $state.go( '^' );
      });
    };
  }]);